//
//  ALHandleAliManager.m
//  THB
//
//  Created by 哀木涕 on 2016/12/16.
//  Copyright © 2016年 方诺科技. All rights reserved.
//

#import "ALHandleAliManager.h"
#import <AlibcTradeSDK/AlibcTradeSDK.h>
#import <AlibcTradeSDK/AlibcTradeShowParams.h>
#import <AlibcTradeSDK/AlibcTradeTaokeParams.h>
#import <AlibcTradeSDK/AlibcTradeService.h>
#import <AlibabaAuthSDK/ALBBSDK.h>
#import "ALGoodsModel.h"
@implementation ALHandleAliManager

+ (tradeProcessSuccessCallback)tradeProcessSuccessCallback{
    
    return ^(AlibcTradeResult *tradeProcessResult){
        NSError *error = nil;
        NSData * JSONData = [NSJSONSerialization dataWithJSONObject:tradeProcessResult.payResult.paySuccessOrders options:kNilOptions error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
        [self postOrderMethod:jsonString];
    };
}
+ (tradeProcessFailedCallback)tradeProcessFailedCallback{
    return ^(NSError *error){
        NSLog(@"打开商品详细失败原因==%@",error);
    };
}

+ (void)postOrderMethod:(NSString *)orderNum{
    ALGoodsModel *goods = [[ALGoodsModel alloc]init];
    goods.order_id = orderNum;
    [goods post:^(ALBaseRespModel *respModel) {
        
    }];
    
}
+ (NSDictionary *)trackParams {
    return @{@"track_key": @"track_value"};
}

+ (void)openWithGoodsId:(NSString *)goodsId control:(UIViewController *)control{
    id<AlibcTradePage> page = [AlibcTradePageFactory itemDetailPage:goodsId];
    id<AlibcTradeService> service = [AlibcTradeSDK sharedInstance].tradeService;
    AlibcTradeShowParams *showParams = [[AlibcTradeShowParams alloc] init];
    showParams.openType = ALiOpenTypeAuto;
    [service show:control page:page showParams:showParams taoKeParams:nil trackParam:[self trackParams]
     tradeProcessSuccessCallback:[self tradeProcessSuccessCallback] tradeProcessFailedCallback:[self tradeProcessFailedCallback]];
}

+ (void)openWithUrl:(NSString *)url control:(UIViewController *)control{
    id<AlibcTradePage> page = [AlibcTradePageFactory page:url];
    id<AlibcTradeService> service = [AlibcTradeSDK sharedInstance].tradeService;
    AlibcTradeShowParams *showParams = [[AlibcTradeShowParams alloc] init];
    showParams.openType = ALiOpenTypeAuto;
    [service show:control page:page showParams:showParams taoKeParams:nil trackParam:[self trackParams] tradeProcessSuccessCallback:[self tradeProcessSuccessCallback] tradeProcessFailedCallback:[self tradeProcessFailedCallback]];
}

@end
